package com.woniu.cms.contract.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.contract.core.entity.AreaVo;
import com.woniu.cms.contract.core.entity.Public;
import com.woniu.cms.contract.core.entity.UndertakeContract;
import com.woniu.cms.contract.core.mapper.PublicMapper;
import com.woniu.cms.contract.core.service.IBidService;
import com.woniu.cms.contract.core.service.IPublicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.api.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author maple leaf
 * @since 2022-09-17
 */
@Service
public class PublicServiceImpl extends ServiceImpl<PublicMapper, Public> implements IPublicService {

    @Autowired
    private PublicMapper publicMapper;

    @Autowired
    private IBidService bidService;


    @Override
    public Result queryPublic(String undertakeContractCode, String publicCode, Integer currentPage, Integer pageSize) {

        Page<Public> page = new Page<>(currentPage,pageSize);

        QueryWrapper<Public> wrapper = new QueryWrapper<>();

        if (undertakeContractCode != null && undertakeContractCode != ""){
            wrapper.like("undertake_contract_code",undertakeContractCode);
        }

        if (publicCode != null && publicCode != ""){
            wrapper.like("public_code",publicCode);
        }
        Page<Public> publicPage = this.page(page, wrapper);

        List<Public> records = publicPage.getRecords();
        long total = publicPage.getTotal();

        HashMap<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);

        return Result.common(map);
    }

    @Override
    public Result addPublic(Public publi) {

        publi.setSubmitTime(new Date());

        //设置时间格式，截取时间年份
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        //将时间转成String
        String date = format.format(new Date());
        //根据用户输入地区，查询地区的编号
        String region = publi.getRegion();
        AreaVo areaVo = bidService.areaName(region);

        //截取编号后四位
        String substring = areaVo.getAreaId().substring(2, 6);

        //使用年份+编号进行模糊查询，得到list集合
        List<Public> publics = this.generatedNumber("PU", date, substring);

        //判断集合是否有数据
        if (publics.size() == 0){
            //数据库没有数据则自添一条
            publi.setPublicCode("UN" + date + substring + "001");
            System.out.println(publi.getPublicCode());
        }else{
            //拿到集合的第一条数据
            Public contract = publics.get(0);
            System.out.println("contract = " + contract);

            //拿到数据中最新的投标编号
            String unCode = contract.getPublicCode();
            System.out.println("code = " + unCode);
            //截取字段进行增加
            String substring1 = unCode.substring(6, unCode.length());
            Long i = Long.valueOf(Integer.parseInt(substring1));
            ++i;
            System.out.println("i = " + i);
            String s = String.valueOf(i);
            //截取字段，进行合并，添加到对象中
            String substring2 = unCode.substring(0, 7);
            String s2 = substring2 + s;
            System.out.println("s2 = " + s2);
            publi.setPublicCode(s2);
        }

        boolean b = this.save(publi);
        return b == true ? Result.ok() : Result.error();
    }


    /*
    * 编号查询
    * */
    @Override
    public List<Public> generatedNumber(String pu, String date, String area) {
        List<Public> publics = publicMapper.generatedNumber(pu, date, area);
        return publics;
    }


    @Override
    public Result auditPublic(Public publi) {
        QueryWrapper<Public> wrapper = new QueryWrapper<>();

        wrapper.eq("public_code",publi.getPublicCode());
        boolean b = this.update(publi, wrapper);

        return b == true ? Result.ok() : Result.error();
    }

    @Override
    public Result delPublic(String publicCode) {

        QueryWrapper<Public> wrapper = new QueryWrapper<>();

        wrapper.eq("public_code",publicCode);
        boolean b = this.remove(wrapper);

        return b == true ? Result.ok() : Result.error();
    }

    @Override
    public Result queryByPublic(String publicCode) {
        QueryWrapper<Public> wrapper = new QueryWrapper<>();

        wrapper.eq("public_code",publicCode);
        Public one = this.getOne(wrapper);
        return Result.common(one);
    }


}
